﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using MyFloridaHouse.libSvOpenDistricts.SummaryDataGrid;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Documents;
using System.Windows.Markup;



// based partly on http://elegantcode.com/2010/03/08/silverlight-datagrid-populate-dynamic-columns-from-a-child-collection/

namespace MyFloridaHouse.SilverlightOpenDistricts
{
    public partial class MainPage
    {
        private void InitializeDistrictDataSummaryEvents()
        {
            SummaryDataGridManager.SummaryDataGridDatasetCriteriaChanged += OnSummaryDataGridDatasetCriteriaChange;
            // set up the data grid display for the first time
            SetUpSummaryDataGrid();
        }

        private void OnSummaryDataGridDatasetCriteriaChange(object sender, EventArgs e)
        {
            SetUpSummaryDataGrid();
        }

        private void SetUpSummaryDataGrid()
        {
            // clear out the old columns and re-build them with new criteria
            districtSummaryDataGrid.Columns.Clear();

            // Set up the DataGrid to match the district data summary columns from (defined in the criteria dataset).
            int index = 0;
            foreach (SummaryDataGridColumnDescription column in viewModelBase.dataGridManager.summaryDataGridColumnDescriptionList)
            {
                districtSummaryDataGrid.Columns.Add(CreateTextColumn(index++, column.name, column.header, column.description, column.isFrozen));
            }

            if (viewModelBase.districtPlan.districtCount > 0)
            {
                viewModelBase.BuildDistrictSummaryAll();
            }
        }

        private DataGridTextColumn CreateTextColumn(int index, string fieldName, string header, string description, bool isFrozen)
        {
            DataGridTextColumn column = new DataGridTextColumn();
            //TODO: column.IsFrozen = isFrozen; can't be assigned to, read only property, so how do you assign to it?
            column.Header = header;
            column.HeaderStyle = BuildHeaderStyle(description);
            column.CellStyle = (Style)Resources["SummaryDataGridCellStyle"];
            column.Binding = new Binding("valuesList[" + index + "]");
            return column;
        }

        private static Style BuildHeaderStyle(string tooltip)
        {
            Style newGridHeaderStyle = new Style(typeof(DataGridColumnHeader));
            newGridHeaderStyle.Setters.Add(new Setter { Property = FontSizeProperty, Value = 9.0 });
            newGridHeaderStyle.Setters.Add(new Setter { Property = FontWeightProperty, Value = FontWeights.Bold });
            newGridHeaderStyle.Setters.Add(new Setter { Property = DataGridColumnHeader.ContentTemplateProperty, Value = CreateDataGridHeaderTemplate(tooltip) });
            return newGridHeaderStyle;
        }

        private static DataTemplate CreateDataGridHeaderTemplate(string tooltip)
        {
            string str = @"<DataTemplate xmlns='http://schemas.microsoft.com/client/2007'>"
                        + @"<ContentControl   Content='{Binding}'>"
                        + @"<ToolTipService.ToolTip>"
                        + @"<ToolTip Content='" + tooltip + "'>"
                        + @"</ToolTip>"
                        + @"</ToolTipService.ToolTip>"
                        + @"</ContentControl >"
                        + @"</DataTemplate>";
            return (DataTemplate)XamlReader.Load(str);
        }
    }
}
